package graphics.view3D;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Hashtable;

import model.Piece;
import graphics.interfaces.IDrawable;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Piece3D implements IDrawable {
	private Piece type;
	//private Sprite sprite;
	private Texture pieceSet;
	private AnimationType currentAnimationType;
	private Cardinals orientation;
	private int xAnimationStart;
	private int yAnimationStart;
	private int xAnimationEnd;
	private int yAnimationEnd;
	private int xAnimationCurrent;
	private int yAnimationCurrent;	
	private long animationStartTime;
	private long animationEndTime;
	
	private Hashtable<AnimationType, ArrayList<Sprite>> sprites;
		
	public Piece3D(Piece type, String chessSet3D){
		this.type = type;
		sprites = new Hashtable<AnimationType, ArrayList<Sprite>>();
		// TODO replace by directory
		this.currentAnimationType = AnimationType.STATIC;
		LoadContent();
	}
	
	private int WHITE = 0;
	private int BLACK = 1;
	
	// TODO load sprites at Board3Dview or Board3D level
	public void LoadContent() {
		
		Color customWhite = new Color(1f, 0.3f, 0.3f, 1f);
		Color customBlue = new Color(0.3f, 0.3f, 1f, 1f);
		
		switch(type){
		case WP:
			loadSprites("pawn",WHITE);
			break;
		case WR:
			loadSprites("rook",WHITE);
			break;
		case WN:
			loadSprites("knight",WHITE);
			break;
		case WB:
			loadSprites("bishop",WHITE);
			break;
		case WQ:
			loadSprites("queen",WHITE);
			break;
		case WK:
			loadSprites("king",WHITE);
			break;
		case BP:
			loadSprites("pawn",BLACK);
			break;
		case BR:
			loadSprites("rook",BLACK);
			break;
		case BN:
			loadSprites("knight",BLACK);
			break;
		case BB:
			loadSprites("bishop",BLACK);
			break;
		case BQ:
			loadSprites("queen",BLACK);
			break;
		case BK:
			loadSprites("king",BLACK);
			
			break;
		default:
			break;
		}
	}
	
	private void loadSprites(String string, int color) {
		sprites.clear();
		
		
		//this.sprite = new Sprite(this.pieceSet, 120, 60, 60, 60);
		//this.sprite.setColor(customBlue);
	}

	/**
	 * Draw current sprite at current position
	 */
	public void Draw(SpriteBatch spriteBatch) {
		this.updateCurrentPosition();
		Sprite sprite = getCurrentSprite();
		int x = getCurrentPositionX();
		int y = getCurrentPositionY();
		sprite.setPosition(x, y);
		sprite.draw(spriteBatch);
	}
		
	private Sprite getCurrentSprite() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * Set draw coordinates
	 * @param x
	 * @param y
	 */
	/*
	public void SetPosition(int x, int y){
		this.sprite.setPosition(x, y);
	}
	*/
	
	public void SetStartPosition(int x, int y){
		xAnimationStart = x;
		yAnimationStart = y;
	}
	public void SetEndPosition(int x, int y){
		xAnimationEnd = x;
		yAnimationEnd = y;
	}
	
	
	public void setAnimationType(AnimationType animationType){
		this.currentAnimationType = animationType;
	}
	
	public void updateCurrentPosition() {
		long currentTime = System.currentTimeMillis();
		if(!(this.currentAnimationType == AnimationType.STATIC)) {	
			if(currentTime<animationEndTime){
				xAnimationCurrent = (int) (xAnimationStart + (currentTime-animationStartTime)*(xAnimationEnd-xAnimationStart)/(animationEndTime-animationStartTime));
				yAnimationCurrent = (int) (yAnimationStart + (currentTime-animationStartTime)*(yAnimationEnd-yAnimationStart)/(animationEndTime-animationStartTime));
			}else{
				xAnimationCurrent = xAnimationEnd;
				yAnimationCurrent = yAnimationEnd;
			}
			
		}
	}
	
	public int getCurrentPositionX() {
		return xAnimationCurrent;
	}
	public int getCurrentPositionY() {
		return yAnimationCurrent;
	}
	
}